package com.wsc.wisdomchina.ai.adapter;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.wsc.wisdomchina.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 自动填充
 * 
 * @author 沈宇坤
 * @time 下午1:12:26
 */
public class AutoAdapter extends BaseAdapter implements Filterable {
	/** 上下文对象 */
	private Context mContext = null;
	/** 数据源 */
	private List<String> mList = null;
	/** 过滤器 */
	private MyFilter mFilter = null;
	/** 复用容器 */
	private ViewHolder mViewHolder = null;
	/** 检索文字 */
	private String prefixString = "";
	/**
	 * 构造函数
	 * 
	 * @param mContext
	 *            上下文对象
	 * @param mList
	 *            数据源
	 */
	public AutoAdapter(Context mContext, List<String> mList) {
		super();
		this.mContext = mContext;
		this.mList = mList;
	}

	@Override
	public Filter getFilter() {
		if (mFilter == null) {
			return new MyFilter();
		} else {
			return mFilter;
		}

	}

	/**
	 * 更新数据源
	 * 
	 * @param mList
	 *            数据源
	 */
	public void updateList(List<String> mList, String prefixString) {
		this.mList = mList;
		this.prefixString = prefixString;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (mList.size() == 0) {
			return 0;
		} else {
			return mList.size();
		}

	}

	@Override
	public Object getItem(int position) {
		if (mList.size() == 0) {
			return null;
		} else {
			return mList.get(position);
		}
	}

	@Override
	public long getItemId(int position) {
		if (mList.size() == 0) {
			return 0;
		} else {
			return position;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			mViewHolder = new ViewHolder();
			// 布局文件
			convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_auto_item, parent, false);
			// 要显示文字的TextView
			mViewHolder.mItemTitle = (TextView) convertView.findViewById(R.id.itemTitle);
			convertView.setTag(mViewHolder);
		} else {
			mViewHolder = (ViewHolder) convertView.getTag();
		}
		if (prefixString.isEmpty() || mList.isEmpty()) {
			mViewHolder.mItemTitle.setText(mList.get(position));
		} else {
			// 找出相同的文字的Index
			List<Integer> start = new ArrayList<>();
			List<Integer> end = new ArrayList<>();
			for (int i = 0; i < prefixString.length(); i++) {
				String compare = prefixString.substring(i, i + 1);
				for (int j = 0; j < mList.get(position).length(); j++) {
					String compare2 = mList.get(position).substring(j, j + 1);
					if (compare.equals(compare2)) {
						start.add(j);
						end.add(j + 1);
					}
				}
			}
			SpannableStringBuilder result = new SpannableStringBuilder((position+1)+"."+mList.get(position));
			// 变色
			for (int i = 0; i < start.size(); i++) {
				result.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.title_blue)),
						start.get(i)+2, end.get(i)+2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			}
			mViewHolder.mItemTitle.setText(result);
		}
		return convertView;
	}

	/**
	 * 复用布局
	 */
	private class ViewHolder {
		private TextView mItemTitle;
	}

	/**
	 * 自定义的过滤器
	 */
	class MyFilter extends Filter {

		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			// // 持有过滤操作完成之后的数据。该数据包括过滤操作之后的数据的值以及数量。 count:数量
			// values包含过滤操作之后的数据的值
			FilterResults results = new FilterResults();
			// if (mOriginalValues == null) {
			// synchronized (mLock) {
			// mOriginalValues = new ArrayList<String>();
			// }
			// }
			// if (constraint == null || constraint.length() == 0) {
			// synchronized (mLock) {
			// ArrayList<String> list = new ArrayList<String>();
			// results.values = list;
			// results.count = list.size();
			// }
			// } else {
			// String prefixString = constraint.toString();
			// for (int i = 0; i < mList.size(); i++) {
			// if (mList.get(i).contains(prefixString)) {
			// mOriginalValues.add(mList.get(i));
			// }
			// }
			// results.values = mOriginalValues;
			// results.count = mOriginalValues.size();
			//
			// }
			return results;
		}

		@Override
		protected void publishResults(CharSequence constraint, FilterResults results) {
			// mList = (List<String>) results.values;
			// if (results.count > 0) {
			// notifyDataSetChanged();
			// } else {
			// notifyDataSetInvalidated();
			// }
		}

	}

}
